﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PaulBeck.Timesheet.BOL
{

    public struct ConsultantResources
    {
        public int ResourceId { get; set; }
        public string Name { get; set; }
    }

    public struct ConsultantHours
    {
        public string Client { get; set; }
        public string Project { get; set; }
        public decimal TotalAllocatedHours { get; set; }
        public decimal TotalExpectedHours { get; set; }
    }

    public static class ConsultantBOL
    {
        public static List<ConsultantResources> GetAllConsultants()
        {
            TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
            var resourceAll = from res in dc.Resources
                              select new ConsultantResources
                              {
                                  ResourceId = res.ResourceId,
                                  Name = res.Name,
                              };
            return resourceAll.ToList();
        }

        public static int GetConsultantIdByLoginKey(string loginName)
        {
            TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
            var resource = (from res in dc.Resources
                            where res.UserId == loginName
                            select new
                            {
                                res.ResourceId,
                            }).SingleOrDefault();
            if (resource == null)
            {
                return 0;
            }
            else
            {
                return resource.ResourceId;
            }
        }

        public static List<ConsultantHours> GetClientProjectHoursByConsultantId(int UserId)
        {

            TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
            var client = from c in dc.Clients
                         join p in dc.Projects on c.ClientId equals p.ClientId
                         join pr in dc.ProjectResources on p.ProjectId equals pr.ProjectId
                         where pr.ResourceId == UserId
                         select new ConsultantHours
                         {
                             Client = c.Name,
                             Project = p.Description,
                             TotalAllocatedHours = pr.TotalAllocatedHours,
                             TotalExpectedHours = pr.TotalExpectedHours,
                         };
            return client.ToList();
        }

        public static int GetWeekEntryIdIdByUserIdForDay(int UserId, DateTime SelectedDateTime)
        {
            TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
            var week = (from w in dc.WeekEntries
                        where w.ResourceId == UserId
                         && (SelectedDateTime == w.WeekStartDate)
                        select new
                        {
                            w.WeekEntryId
                        }).Single();
            return week.WeekEntryId;
        }

    }
}
